package com.yonyou.brigade.openapi.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.order_center.service.IInvoiceRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 发票订单关联表 前端控制器
 * </p>
 *
 * @author houyb
 * @since 2020-03-03
 */
@Controller
@RequestMapping("/invoiceRelationship")
public class InvoiceRelationshipController {

    @Autowired
    private IInvoiceRelationshipService relationshipService;

    /**
     * 匹配发票和订单
     * @param matchData
     * @return
     */
    @RequestMapping(value = "/match" , method = RequestMethod.POST)
    @ResponseBody
    public JSONObject invoiceMatchOrder(@RequestBody JSONObject matchData){
        String tenant = matchData.getString("tenantId");
        String userId = matchData.getString("userId");
        return relationshipService.invoiceMatchOrder(tenant, userId,  matchData);
    }

    /**
     * 手动关联或取消关联票据和订单
     * @param param
     * @return
     */
    @RequestMapping(value = "/manualMatching" , method = RequestMethod.POST)
    @ResponseBody
    public JSONObject manualMatching(@RequestBody JSONObject param){
        JSONArray invoicesBrigades = param.getJSONArray("invoicesBrigades");
        String action = param.getString("action");
        JSONObject result = new JSONObject();
        boolean success = relationshipService.manualMatching(action, invoicesBrigades);;
        result.put("success", success);
        StringBuilder msg = new StringBuilder();
        msg.append("手动");
        if ("match".equals(action)){
            msg.append("匹配");
        }else {
            msg.append("取消匹配");
        }
        String str  = success ? "成功" : "失败";
        msg.append(str);
        result.put("msg", msg.toString());
        return result;
    }
}

